package com.zswl.decorate;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.bumptech.glide.request.target.ViewTarget;
import com.zswl.decorate.util.Density;
import com.zswl.decorate.util.SpUtil;

import java.util.HashSet;
import java.util.Set;

import io.flutter.app.FlutterApplication;

/**
 * Created by Administrator on 2018/7/16 0016.
 */

public class App extends Application implements Application.ActivityLifecycleCallbacks {
    private static App instance;
    private Set<Activity> allActivities;
    @Override

    public void onCreate() {
        super.onCreate();
        instance = this;

        registerActivityLifecycleCallbacks(this);
        SpUtil.init(this);
        ViewTarget.setTagId(R.id.tag_glide);//Glide配置
//        NineGridView.setImageLoader(new NineImageLoader());
//        CrashHandler.getInstance().init(this);
        Density.setDensity(this, 360);//屏幕适配

    }
//
//    @Override
//    protected void attachBaseContext(Context base) {
//        super.attachBaseContext(base);
//        MultiDex.install(this) ;
//    }


    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        addActivity(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        removeActivity(activity);
    }



    public static App getAppInstance() {
        return instance;
    }

    public void addActivity(Activity act) {
        if (allActivities == null) {
            allActivities = new HashSet<>();
        }
        allActivities.add(act);
    }

    public void removeActivity(Activity act) {
        if (allActivities != null) {
            allActivities.remove(act);
        }
    }

    public void finishAllActivity() {
        if (allActivities != null) {
            synchronized (allActivities) {
                for (Activity act : allActivities) {
                    act.finish();
                }
            }
        }
    }

    public void exitApp() {
        finishAllActivity();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }
}
